/*!
 Temelia - generic algorithms interface.

 Copyright (C) 2008, 2009 Ceata (http://ceata.org/proiecte/temelia).

 @author Dascalu Laurentiu, Bercaru Cristian

 This program is free software; you can redistribute it and
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 3
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#ifndef ALGORITHMS_H_
#define ALGORITHMS_H_

#include "platform.h"

/*!
 * @brief For an indexable collection X = {x[i]} calls iterate_function
 * for-each key x[i]. The initial collections may be modified only in iterate_function.
 * O(n) complexity
 *
 * @param Pointer to indexable collection
 * @param Collection's size
 * @param Iterate function
 * @param Context
 */
DECLSPEC void map(void *data, int size, void *key_at(void *data, int index),
		void iterate_function(void *key, void *context), void *context);

/*!
 * @brief For an indexable collection X = {x[i]} calls filter_function
 * for-each key in context0. If filter function returns true then applies
 * then_function to * x[i] in context1; else applies else_function to x[i] in
 * context2.
 * O(n) complexity
 *
 * @param Pointer to indexable collection
 * @param Collection's size
 * @param Iterate function
 * @param Elements indexing function
 * @param Then function
 * @param Else function
 * @param Context for filter function
 * @param Context for then function
 * @param Context for else function
 */
DECLSPEC void filter(void *data, int size, void *key_at(void *data, int index),
		int filter_function(void *key, void *context), void then_function(
				void *key, void *context), void else_function(void *key,
				void *context), void *filter_context, void *then_context,
		void *else_context);
/*!
 * @brief Searches key in a sorted collection using binary search algorithm and
 * returns the index if key is found and -1 otherwise.
 * O(log(n)) complexity
 *
 * @param Pointer to indexable collection
 * @param Collection's size
 * @param Key to find
 * @param Index function
 * @param Comparison function
 */
DECLSPEC int
		binary_search(void *data, int size, void *key, void *key_at(
				void *data, int index), int compare(void *x, void *y,
				void *context), void *context);

#endif /* ALGORITHMS_H_ */
